package com.haochang.chunkvod.controller.activity.hostess;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.controller.fragment.hostess.DeskOpenFragment;
import com.haochang.chunkvod.controller.fragment.hostess.DeskSettingFragment;
import com.haochang.chunkvod.controller.fragment.hostess.DeskSubFragment;
import com.haochang.chunkvod.controller.fragment.hostess.DeskToNoneFragment;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.view.vod.title.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 *         created at 2016/8/24 18:02
 *         桌台操作页面
 */
public class DeskManagerActivity extends BaseActivity
{
	private List<BaseFragment> listFragments = new ArrayList<BaseFragment>();//viewPager中显示界面的集合
	private ImageView cursorIv;// 选项下面的图片
	private BaseTextView tv_room_manager, tv_subcontract, tv_desk_to_none, tv_room_clear;//选项
	private List<BaseTextView> titles = new ArrayList<BaseTextView>();//存储选项的集合
	private ViewPager viewPager;//内容框
	private int offset ;//偏移量（手机屏幕宽度 / 选项卡总数 - 选项卡长度） / 2
	private int lineWidth;//下划线图片宽度
	private int current_index ;//当前选项卡的位置
	private int oldPosition ;//viewPager上一次位置
	//选项卡索引
	private static final int TAB_COUNT = 4;

	private static final int TAB_MANAGER = 0;
	private static final int TAB_SUB = 1;
	private static final int TAB_SETTING = 2;
	private static final int TAB_CLEAR = 3;
	//房间状态
	private String roomStatue;
	//房号
	private String roomNum;
	//房间编号
	private String roomId;

	private DeskOpenFragment deskOpenFragment;//开台
	private DeskSubFragment deskSubFragment;//转台
	private DeskToNoneFragment deskToNoneFragment;//转空台
	private DeskSettingFragment deskSettingFragment;//桌台设置

	private String roomUsage;//房间用途
	private String billId;//账单号
	private String roomTypeId;//房间类型id
	private String roomType;//房型名称

	private int startIndex;

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		roomNum = getIntent().getStringExtra(IntentKey.ROOM_NUM);
		roomStatue = getIntent().getStringExtra(IntentKey.ROOM_STATUS);
		roomId = getIntent().getStringExtra(IntentKey.ROOM_ID);
		roomUsage = getIntent().getStringExtra(IntentKey.ROOM_USAGE);
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		roomTypeId = getIntent().getStringExtra(IntentKey.ROOM_TYPE_ID);
		roomType = getIntent().getStringExtra(IntentKey.ROOM_TYPE);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_desk_manager);
		initUI();
		initTitlesAndViewsByStatue();
		initImageView();
		initViewPager();
	}

	/**
	 * 初始化布局和监听
	 */
	private void initUI()
	{
		TitleView titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(roomNum).setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{

			}

		});
		viewPager = (ViewPager) findViewById(R.id.vPager);
		cursorIv = (ImageView) findViewById(R.id.iv_tab_bottom_img);
		tv_room_manager = (BaseTextView) findViewById(R.id.tv_room_manager);
		tv_room_manager.setTag(TAB_MANAGER);
		tv_subcontract = (BaseTextView) findViewById(R.id.tv_subcontract);
		tv_subcontract.setTag(TAB_SUB);
		tv_desk_to_none = (BaseTextView) findViewById(R.id.tv_desk_to_none);
		tv_desk_to_none.setTag(TAB_SETTING);
		tv_room_clear = (BaseTextView) findViewById(R.id.tv_room_clear);
		tv_room_clear.setTag(TAB_CLEAR);
		tv_room_manager.setOnClickListener(onBarBaseClickListener);
		tv_subcontract.setOnClickListener(onBarBaseClickListener);
		tv_desk_to_none.setOnClickListener(onBarBaseClickListener);
		tv_room_clear.setOnClickListener(onBarBaseClickListener);

		Bundle bundle = new Bundle();
		bundle.putString(IntentKey.ROOM_ID, roomId);
		bundle.putString(IntentKey.ROOM_STATUS, roomStatue);
		bundle.putString(IntentKey.BILL_ID, billId);
		bundle.putString(IntentKey.ROOM_TYPE_ID, roomTypeId);
		bundle.putString(IntentKey.ROOM_TYPE, roomType);
		bundle.putString(IntentKey.ROOM_USAGE, roomUsage);
		bundle.putString(IntentKey.COMSTMER_COUNT, getIntent().getStringExtra(IntentKey.COMSTMER_COUNT));
		bundle.putString(IntentKey.REMARKS, getIntent().getStringExtra(IntentKey.REMARKS));

		deskOpenFragment = new DeskOpenFragment();
		deskOpenFragment.setArguments(bundle);

		deskSubFragment = new DeskSubFragment();
		deskSubFragment.setArguments(bundle);

		deskToNoneFragment = new DeskToNoneFragment();
		deskToNoneFragment.setArguments(bundle);

		deskSettingFragment = new DeskSettingFragment();
		deskSettingFragment.setArguments(bundle);

	}

	/**
	 * 初始化底部下划线
	 */
	private void initImageView()
	{
		// 获取图片宽度
		lineWidth = BitmapFactory.decodeResource(getResources(), R.drawable.ic_tab).getWidth();
		// 用于获取屏幕分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		// 获取分辨率宽度
		offset = (screenW / TAB_COUNT - lineWidth) / 2;

		if (TextUtils.equals(roomStatue, "using"))
		{//桌台使用中
			startIndex = 1;
		} else
		{
			startIndex = Integer.parseInt(titles.get(0).getTag().toString());
		}
		current_index = startIndex;
		int toX = startIndex * (offset * 2 + lineWidth) + offset;
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) cursorIv.getLayoutParams();
		params.setMargins(toX, 0, 0, 0);
		cursorIv.setLayoutParams(params);
		if (titles.size() > 0)
		{
			titles.get(oldPosition).setTextColor(Color.RED);
		}
	}

	/**
	 * 初始化ViewPager并添加监听事件
	 */
	private void initViewPager()
	{
		if (listFragments != null)
		{
			viewPager.setAdapter(new DeskManagerFragmentPagerAdapter(this.getSupportFragmentManager(), listFragments));
			viewPager.setCurrentItem(oldPosition);
			viewPager.setOffscreenPageLimit(titles.size());
			viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
			{
				int quarterWidth = offset * 2 + lineWidth;//四分之一屏幕宽度

				@Override
				public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
				{

				}

				@Override
				public void onPageSelected(int position)
				{
					// 下划线开始移动前的位置
					float fromX = quarterWidth * (current_index - startIndex) + offset;
					// 下划线移动完毕后的位置
					BaseTextView toText = titles.get(position);
					int toIndex = Integer.parseInt(toText.getTag().toString()) - startIndex;
					int toX = quarterWidth * toIndex;

					Animation animation = new TranslateAnimation(fromX, toX, 0, 0);
					animation.setFillAfter(true);
					animation.setDuration(100);
					// 给图片添加动画
					cursorIv.startAnimation(animation);
					// 当前Tab的字体变成红色
					if (titles != null && titles.size() > position && titles.size() > oldPosition)
					{
						titles.get(oldPosition).setTextColor(Color.BLACK);
						titles.get(position).setTextColor(Color.RED);
					}
					current_index = Integer.parseInt(toText.getTag().toString());
					oldPosition = position;
				}

				@Override
				public void onPageScrollStateChanged(int state)
				{

				}
			});
		}
	}

	/**
	 * 根据桌台状态设置可选中的选项卡
	 */
	private void initTitlesAndViewsByStatue()
	{
		if (!TextUtils.isEmpty(roomStatue))
		{
			if (titles == null)
			{
				titles = new ArrayList<BaseTextView>();
			}
			if (listFragments == null)
			{
				listFragments = new ArrayList<BaseFragment>();
			}
			titles.clear();
			listFragments.clear();
			if (TextUtils.equals("using", roomStatue))
			{//桌台在使用
					//				titles.add(tv_room_manager);
				titles.add(tv_subcontract);
				//				titles.add(tv_desk_to_none);
				titles.add(tv_room_clear);
				//				listFragments.add(deskOpenFragment);
				listFragments.add(deskSubFragment);
				//				listFragments.add(deskToNoneFragment);
				listFragments.add(deskSettingFragment);
			} else if (TextUtils.equals("empty", roomStatue))
			{//桌台 为空
				titles.add(tv_room_manager);
				listFragments.add(deskOpenFragment);
			} else if (TextUtils.equals("paied", roomStatue))
			{//桌台 已支付/买单未离
				titles.add(tv_desk_to_none);
				listFragments.add(deskToNoneFragment);
			}
		}
	}

	private OnBaseClickListener onBarBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			int len = titles.size();
			for (int i = 0; i < len; i++)
			{
				Logger.e("点击的tag：" + v.getTag().toString() + "  第ige:" + titles.get(i).getTag().toString());
				if (TextUtils.equals(v.getTag().toString(), titles.get(i).getTag().toString()))
				{//点击的tag与第i个tag一样
					viewPager.setCurrentItem(i);
					return;
				}
			}
		}
	};

	/**
	 * ViewPager适配器
	 */
	public class DeskManagerFragmentPagerAdapter extends FragmentPagerAdapter
	{

		List<BaseFragment> fragmentList = new ArrayList<BaseFragment>();

		public DeskManagerFragmentPagerAdapter(FragmentManager fm, List<BaseFragment> fragmentList)
		{
			super(fm);
			this.fragmentList = fragmentList;
		}

		@Override
		public Fragment getItem(int position)
		{
			return null == fragmentList ? null : fragmentList.get(position);
		}

		@Override
		public int getCount()
		{
			return null == fragmentList ? 0 : fragmentList.size();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			finish();
			animRightToLeft();
		}
		return super.onKeyDown(keyCode, event);
	}
}
